/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2016 All Rights Reserved.
 */
package com.fulihui.information.service;

import com.fulihui.information.api.GoodFeedbackService;
import com.fulihui.information.core.repository.GoodFeedbackRepository;
import com.fulihui.information.dto.InfoGoodFeedbackRecordDTO;
import com.fulihui.information.enums.GoodFeedbackStatusEnum;
import com.fulihui.information.request.*;
import com.fulihui.usercore.api.UserService;
import com.fulihui.usercore.dto.UserDTO;
import com.fulihui.usercore.request.user.UserSingleRequest;
import com.google.common.collect.Maps;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.error.Errors;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.result.TPageResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.servicesupport.util.ServiceAssert;
import org.near.toolkit.common.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static org.near.servicesupport.result.ResultBuilder.succ;

/**
 * 有益反馈 服务实现
 *
 * @author zhangyingjie
 * @version $Id: v 0.1 2017年05月15日 14:56 zhangyingjie Exp $
 */
@Service(version = "1.0.0")
public class GoodFeedbackServiceImpl implements GoodFeedbackService {
    private Logger log = LoggerFactory.getLogger("GOOD_FEEDBACK");
    @Resource
    private GoodFeedbackRepository goodFeedbackRepository;


    @Reference(version = "1.0.0", check = false)
    private UserService userService;

    @Override
    public BaseResult create(GoodFeedbackCreateRequest request) {

        InfoGoodFeedbackRecordDTO dto = new InfoGoodFeedbackRecordDTO();
        BeanUtils.copyProperties(request, dto, "id", "status", "operator", "gmtCreate", "gmtModified");
        //新加入 默认为 待审核
        dto.setStatus(GoodFeedbackStatusEnum.PRE_AUDIT.getCode());
        dto.setGmtCreate(new Date());
        boolean insert = goodFeedbackRepository.create(dto);
        ServiceAssert.isTrue(insert, Errors.Commons.SYSTEM_ERROR);
        return succ();
    }

    @Override
    public TSingleResult<InfoGoodFeedbackRecordDTO> querySingle(GoodFeedbackDetailRequest request) {
        return ResultBuilder.succTSingle(goodFeedbackRepository.querySingle(request.getId()));
    }

    @Override
    public BaseResult update(GoodFeedbackUpdateRequest request) {

        InfoGoodFeedbackRecordDTO dto = new InfoGoodFeedbackRecordDTO();
        BeanUtils.copyProperties(request, dto);
        boolean update = goodFeedbackRepository.update(dto);
        ServiceAssert.isTrue(update, Errors.Commons.SYSTEM_ERROR);
        return succ();
    }

    @Override
    public TPageResult<InfoGoodFeedbackRecordDTO> queryPage(GoodFeedbackQueryPageRequest request) {
        List<InfoGoodFeedbackRecordDTO> dtos = goodFeedbackRepository.queryPage(request);
        int pageCount = goodFeedbackRepository.queryPageCount(request);
        return ResultBuilder.succTPage(dtos, request.getPage(), request.getRows(),
                pageCount);
    }

    @Override
    public BaseResult batchAudit(GoodFeedbackRecordBatchAuditRequest request) {

        List<InfoGoodFeedbackRecordDTO> recordDTOs = goodFeedbackRepository.queryByIds(request.getIds());
        if (!CollectionUtils.isEmpty(recordDTOs)) {
            //更新未 已审核状态
            if (StringUtil.equals(request.getStatus(), GoodFeedbackStatusEnum.GOOD.getCode())) {
                //先去数据库中更新，然后异步去处理
                if (goodFeedbackRepository.batchUpdateByIds(request.getIds(), request.getStatus(), request.getOperator())) {
                    UserSingleRequest userSingleRequest = new UserSingleRequest();
                    for (InfoGoodFeedbackRecordDTO recordDTO : recordDTOs) {
                        userSingleRequest.setLoginId(recordDTO.getPhone());
                        TSingleResult<UserDTO> userDTOTSingleResult = userService.querySingle(userSingleRequest);
                        if (userDTOTSingleResult.getValue() != null) {
                            /**
                             * 积分返利
                             */
                            Map<String, Object> msg = Maps.newHashMap();
                            msg.put("userId", userDTOTSingleResult.getValue().getUserId());
                            msg.put("outTradeNo", "GOOD_FEEDBACK_" + recordDTO.getId());
                        } else {
                            log.info("根据手机号查询userid出错，手机号:{}", recordDTO.getPhone());
                        }
                    }
                    return succ();
                } else {
                    log.info("批量执行有益反馈审核出错！");
                    return ResultBuilder.fail(500, "批量执行有益反馈审核出错！");
                }
            }
        } else {
            log.info("没有要审核的记录！");
            return ResultBuilder.fail(500, "没有要审核的记录！");
        }
        return succ();
    }
}
